/**********************
**intellistrip.inc
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/
/*
This program is based off a program with the line:
Copyright(C) Mark Zehner/Peter Baumann 1999
*/

//Haven't suppported other compilers yet
#ifndef __GNUC__
#error Only GCC/G++ supported at this time
#endif

#ifdef WIN_32
#error Windows target not supported at this time
#endif

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <string>
#include "intellistrip.h"
#include "intellistrip_serial.h"

using namespace std;

void copyright(void)
{
	printf("*******************************************************************************\n");
	printf("Intellistrip control program\n");
	printf("Copyright 2009 Tomorrow Tech Industries\n");
	printf("Contact: RPITomorrowTech@gmail.com\n");
	printf("*******************************************************************************\n");
	printf("\n");
}

void usage(const char *progName)
{
	copyright();
	printf("Usage: %s <args>\n", progName);
	printf("args:\n");
	
	printf("-g: get port information\n");
	printf("-s: get port power, requires a port\n");
	printf("-s: set port power, requires a port and a power level\n");
	printf("-t: test sequence\n");
	
	printf("-p <port>: select port\n");
	printf("-l <powerLevel>: select power level\n");
}

#define COMMAND__NONE				0
#define COMMAND__GET_INFORMATION	1
#define COMMAND__SET_PORT_POWER		2
#define COMMAND__GET_PORT_POWER		3
#define COMMAND__SET_PASSWORD		4
#define COMMAND__TEST_SEQUENCE		5

int main(int argc, char **argv )
{
	int rc = 1;	
	ttRC rcPrint = TT_GENERAL;
	int port = -1;
	int powerLevel = -1;
	//The action we are to perform;
	int command = COMMAND__NONE;
	string additionalLines;
		
	//Parse input arguments
	for( int argIndex = 1; argIndex < argc; ++argIndex )
	{
		string arg = argv[argIndex];
		/**********
		Commands
		**********/
		if( arg == "-g" )
		{
			command = COMMAND__GET_INFORMATION;
		}
		//get/query port power
		else if( arg == "-q" )
		{
			command = COMMAND__GET_PORT_POWER;
		}
		//set port power
		else if( arg == "-s" )
		{
			command = COMMAND__SET_PORT_POWER;
		}
		//set port power
		else if( arg == "-t" )
		{
			command = COMMAND__TEST_SEQUENCE;
		}
		/*********
		Arguments
		*********/
		//Port
		else if( arg == "-p" && argIndex + 1 < argc )
		{
			port = atoi(argv[argIndex + 1]);
			++argIndex;
		}
		//Level
		else if( arg == "-l" && argIndex + 1 < argc )
		{
			powerLevel = atoi(argv[argIndex + 1]);
			++argIndex;
		}
		/********
		Misc
		********/
		else
		{
			printf_debug("Invalid argument: %s\n", arg.c_str());
			usage(argv[0]);
			goto error;
		}
	}
	
	{
		IntellistripSerial intellistrip;	

		if( TT_FAILED(intellistrip.init()) )
		{
			printf_debug("Failed init\n");
			printf_debug("Last error: %s\n", IntellistripSerial::getLastError().c_str());
			goto error;
		}
			
		switch( command )
		{
		case COMMAND__NONE:
			usage(argv[0]);
			goto error;

		case COMMAND__GET_INFORMATION:
			additionalLines = 
				//vendorName
				"Tomorrow Tech Industries\n"
				//modelNumber
				"Intellistrip 2000\n"
				//modelVersion
				"0.1\n"
				//serialNumber
				"000000001\n"
				//numberPorts
				"5\n"
				//state
				"0\n"
				;
			rcPrint = TT_OK;
			break;

		case COMMAND__SET_PORT_POWER:
			if( port < 0 )
			{
				goto error;
			}
			if( powerLevel < 0 )
			{
				goto error;
			}
			rcPrint = intellistrip.setPortPower(port, powerLevel);
			break;

		case COMMAND__GET_PORT_POWER:
			{
				char buff[16];
				if( port < 0 )
				{
					goto error;
				}
				rcPrint = intellistrip.getPortPower(port, powerLevel);
				snprintf(buff, 16, "%d", powerLevel);
				additionalLines = string(buff) + "\n";
			}
			break;

		case COMMAND__SET_PASSWORD:
			rcPrint = TT_UNSUPPORTED;
			break;

		default:
			printf_debug("Couldn't identify command!\n");
			goto error;
		}
	}		
		
	//Terrible hack to flush the buffers...WHY CAN'T I GET IT TO FLUSH!!?!?!?!
	//Tried fsync, tcdrain
	{
		IntellistripSerial intellistrip;	

		if( TT_FAILED(intellistrip.init()) )
		{
			printf_debug("Failed init\n");
			printf_debug("Last error: %s\n", IntellistripSerial::getLastError().c_str());
			goto error;
		}
	}	

	//First line: rc from intellistrip
	printf("%d\n", rcPrint);
	//Second line: error message, if applicable
	printf("%s\n", IntellistripSerial::getLastError().c_str());
	//Additional lines: specific to command
	printf("%s", additionalLines.c_str());
	
	//We completed at least reasonably well
	rc = 0;

error:
	return rc;
}  

